<?php 
namespace lib\protocol;
class AliQQ_PayMoney{
    /**
     * 获取支付金额信息 云端协议
     * @param string $Type 支付类型，例如 alipay、qqpay 等
     * @param string $Cookie Cookie信息
     * @return array 返回包含支付金额信息的关联数组
     */
    public static function Get_pay_money($Type, $Cookie) {
       
        // 发送请求获取云端协议数据
        $result = get_curl("http://ali.daoh.top/xy/bd/Pay_Money_Api.php?type=$Type&cookie=$Cookie");
    
        // 解析返回的JSON数据
        $result = json_decode($result, true);
    
        // 返回结果
        return $result; 
    }
    /**
     * 获取支付金额信息 本地协议
     * @param string $Type 支付类型，例如 alipay、qqpay 等
     * @param string $Cookie Cookie信息
     * @return array 返回包含支付金额信息的关联数组
     */
    public static function Get_pay_money2($Type = 'alipay',$Cookie){
        $Cookie=base64_decode($Cookie);
        if ($Type == "alipay") {
           $data = self::alipayurl($Cookie);
           $pattern = '/"availableBalance":"(.*?)","cpsAvailableAmount"/';
           preg_match_all($pattern, $data, $trStr);
           if(!$trStr[1][0]){
				$money  = -1;
				$status = false;
			}else{
				$money  = $trStr[1][0];
				$status = true;
			}
			return array("status" => $status,"money" => $money,"time" => time(),"cookie" => $Cookie);
        }
    }
    public static function Get_pay_money3($Type = 'alipay',$Cookie){
        if ($Type == "alipay") {
           $data = self::alipayurl1($Cookie);
           $data['title'] = mb_convert_encoding($data['title'],"UTF-8","GB2312" );
           $trStr[1][0]=$data;
          
			if($trStr[1][0]=="-1"){
				$money  = -1;
				$status = false;
			}else{
				$money  = $trStr[1][0];
				$status = true;
			}
			return array("status" => $status,"money" => $money,"time" => time(),"cookie" => $Cookie);
        }
    }
    
    
    public  static function alipayurl($Cookie){
        switch (rand(1, 12)) {
				case 1:
					$data = self::Get_Alipay_Cookie("https://mrchportalweb.alipay.com/user/home", $Cookie);
					break;
				case 2:
					$data = self::Get_Alipay_Cookie("https://mbillexprod.alipay.com/enterprise/mainIndex.htm#/", $Cookie);
					break;
				case 3:
					$data = self::Get_Alipay_Cookie("https://uemprod.alipay.com/baseinfo/merchantShopBaseInfo.htm#/infomanage?_k=2fp4tc", $Cookie);
					break;
				case 4:
					$data = self::Get_Alipay_Cookie("https://mrchportalweb.alipay.com/dynlink/operationCenter/operateTool.htm", $Cookie);
					break;
				case 5:
					$data = self::Get_Alipay_Cookie("https://mrchportalweb.alipay.com/settling/selfhelp/accessGuide.htm", $Cookie);
					break;
				case 6:
					$data = self::Get_Alipay_Cookie("https://mrchportalweb.alipay.com/user/datacenter/b/home", $Cookie);
					break;
				case 7:
					$data = self::Get_Alipay_Cookie("https://mbillexprod.alipay.com/enterprise/accountTotalAssetQuery.htm#/", $Cookie);
					break;
				case 8:
					$data = self::Get_Alipay_Cookie("https://business.alipay.com/user/assetmanage-oc/user/asset/view_v2", $Cookie);
					break;
				case 9:
					$data = self::Get_Alipay_Cookie("https://mbillexprod.alipay.com/enterprise/mainIndex.htm#/", $Cookie);
					break;
				case 10:
					$data = self::Get_Alipay_Cookie("https://b.alipay.com/signing/productSetV2.htm?mrchportalwebServer=https%3A%2F%2Fmrchportalweb.alipay.com", $Cookie);
					break;
				case 11:
					$data = self::Get_Alipay_Cookie("https://mrchportalweb.alipay.com/operation/centerHome", $Cookie);
					break;
				case 12:
					$data = self::Get_Alipay_Cookie("https://110.alipay.com/sc/study/accountSecurity.htm", $Cookie);
					break;
				default:
					$data = self::Get_Alipay_Cookie("https://business.alipay.com/user/store-management/infomanage", $Cookie);
					break;
			}
			return $data;
    }
    public  static function alipayurl1($Cookie){
        switch(rand(1,9)){
				case 1:
					$data = self::Get_Alipay_Cookie1('https://personalweb.alipay.com/portal/i.htm',$Cookie);
				break;
				case 2:
					$data = self::Get_Alipay_Cookie1('https://my.alipay.com/wealth/index.html',$Cookie);
 				break;
				case 3:
					$data = self::Get_Alipay_Cookie1('https://110.alipay.com/sc/index.htm',$Cookie);
				break;
				case 4:
					$data = self::Get_Alipay_Cookie1('https://my.alipay.com/portal/i.htm',$Cookie);
				break;
				case 5:
					$data = self::Get_Alipay_Cookie1('https://shanghu.alipay.com/home/switchPersonal.htm',$Cookie);
				break;
				case 6:
					$data = self::Get_Alipay_Cookie1('https://cshall.alipay.com/lab/question.htm',$Cookie);
				break;
				case 7:
					$data = self::Get_Alipay_Cookie1('https://cshall.alipay.com/lab/cateQuestion.htm',$Cookie);
				break;
				case 8:
					$data = self::Get_Alipay_Cookie1('https://cshall.alipay.com/lab/help_detail.htm',$Cookie);
				break;
				case 9:
					$data = self::Get_Alipay_Cookie1('https://egg.alipay.com/egg/index.htm',$Cookie);
				break;
				default:
					$data = self::Get_Alipay_Cookie1('http://egg.alipay.com/egg/advice.htm',$Cookie);
				break;
			}
			return $data;
    }
    public  static function Get_Alipay_Cookie($Url_Referer, $Cookie = null){
         $ctoken = getSubstr($Cookie,"ctoken=", ";");
	    $uid =  getSubstr($Cookie,"CLUB_ALIPAY_COM=", ";");
		 $start=date('Y-m-d').'+00:00:00';
	   $end=date('Y-m-d',strtotime('+1day')).'+00:00:00';
	    $Url="https://mbillexprod.alipay.com/enterprise/accountTotalAssetQuery.json?ctoken='.$ctoken.'&_output_charset=utf-8&billUserId=$uid&pageNum=1&pageSize=20&startTime='.$start.'&endTime='.$end.'&status=ALL&sortType=0&_input_charset=gbk";
	     $referer1="https://b.alipay.com/";
		$referer = $Url_Referer.'?&t='.time();
		$ua = 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
cache-control: max-age=0
Cookie: '.@$Cookie.'
referer: '.$Url.'?referer='.$referer.'
$referer1 = "https://mrchportalweb.alipay.com/user/home.htm";
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Linux; Android 10.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 360Browser/9.2.5584.400';
        $result = get_curl($Url_Referer,0,$referer,$Cookie,0,$ua);
        $result = get_curl($Url,0,$referer1,$Cookie,0,$ua);
        return $result;
    }
    public  static  function Get_Alipay_Cookie1($Url_Referer, $Cookie = null)
	{ 
		$Url ="https://shanghu.alipay.com/user/myAccount/index.htm";
		$referer = $Url_Referer.'?&t='.time();
		$ua = 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
cache-control: max-age=0
Cookie: '.@$Cookie.'
referer: '.$Url.'?referer='.$referer.'
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Linux; Android 10.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 360Browser/9.2.5584.400';
		$result = get_curl($Url_Referer,0,$referer,$Cookie,0,$ua);
		$result = mb_convert_encoding(self::Get_Money_curl($Url,0,$referer,$Cookie,0,$ua),"UTF-8","GB2312" );
		if(!strstr($result, '余额'))$result = "-1";else $result = getSubstr($result, '<em class="aside-available-amount">','</em>元</span></li>');
		
		return $result;
	}
	
	public  static function Get_Money_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0){
		if(is_array($cookie))
		{
			$str = '';
			foreach ($cookie as $key => $value){
				$str .= $key.'='.$value.'; ';
			}
			$cookie = substr($str,0,-1);
		}
	$opts = array(
	   'http'=>array(
	   'method'=>($post?'POST':'GET'),
	   'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
				  "Content-length:".strlen($post)."\r\n" . 
				  "Cookie: ".@$cookie."\r\n" . 
				  "\r\n" . $ua .
				  "\r\n",
	   'content' => $post,
	   )
	);
	$context = stream_context_create($opts);
	$ret = file_get_contents($url, false, $context);
	return $ret;
	}
}
?>